package game.game.network.client;

import entity.GameEntity;
import entity.Position;
import game.engine.network.client.ClientServer;
import game.game.network.cmd.CmdCGetCacheMap;
import game.game.network.cmd.CmdCGetObject;
import game.game.network.cmd.CmdSReturnCacheMap;
import game.game.network.cmd.CmdSReturnContext;
import game.game.network.cmd.CmdSReturnPosition;
import game.game.network.cmd.CmdSReturnStaticObject;

public class ControllerClient implements IGameClientController
{
	private IGameClientModel _model;

	private ClientServer _gameClient;

	public ControllerClient(IGameClientModel pModel, ClientServer pGameClient)
	{
		_model = pModel;
		_gameClient = pGameClient;
	}

	private IGameClientModel getModel()
	{
		return (IGameClientModel) _model;
	}

	public void execute( CmdSReturnContext pCmd )
	{
		this.getModel().setContext( pCmd.getContext() );
		// ------------------------------------
		this._gameClient.sendCommand( new CmdCGetCacheMap( this.getModel().getContext(), new Position( 0, 0 ) ) );
		// ------------------------------------
		this._gameClient.sendCommand( new CmdCGetObject( this.getModel().getContext() ) );
	}

	public void execute( CmdSReturnCacheMap pCmd )
	{
		this.getModel().getMap().setFieldEntity( pCmd.getFieldEntity() );

	}

	public void execute( CmdSReturnPosition pCmd )
	{
		GameEntity objEntity = this.getModel().getGameObject( pCmd.getIdObj() );
		if ( objEntity == null )
		{
			objEntity = new GameEntity( pCmd.getIdObj(), new Position( pCmd.getPositionX(), pCmd.getPositionY() ) ,pCmd.getGameEntityType());
			this.getModel().addGameObject( objEntity );
		}
		else
		{
			Position position = objEntity.getPosition();
			position.setX( pCmd.getPositionX() );
			position.setY( pCmd.getPositionY() );
		}

	}

	@Override
	public void execute( CmdSReturnStaticObject pCmd )
	{
		this.getModel().addGameObject( pCmd.getStaticObject() );
	}

}
